<?php
namespace App\Models\Mysql;

use App\Models\Admin\InterfaceConfiguration;
use App\Recharge\Exceptions\CyclePunch\MerchantPriceWithTicketNotSetException;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;

/**
* @comment
*
* @property int $id
* @property int $ids 商品编码
* @property string $goods_code 第三方商品编码
* @property string $goods_name 商品名称
* @property float $unit_price 进价
* @property float $price 销售价
* @property string $time 时间
* @property int $release 1 = 发布  2 = 未发布 3 = 维护
* @property int $goods_belong 商品归属
* @property float $max_price 供应商设置的上限价格
* @property int $is_up 1.上架 2.下架   9=软删除
* @property string $productId 第三方公用参数
* @property int $brand_id 品牌分类id
* @property int $order_times 帐号充值上限
* @property int $order_times_custom 账号默认过期时间
* @property int $timing_is_up 定时上下架开关 1 = 开启  2 = 关闭
* @property int $goods_type 1 = 直冲 2 = 卡密转直冲
* @property int $o_type 1.卡密 2.链接
* @property int $cami_type 卡密状态
* @property int $start_charging 起充数
*
* @property InterfaceConfiguration|null $belong
*/
class XnSupplierGoods extends BaseModel
{

    /**
     * @var string
     */
    protected $table = 'xn_supplier_goods';

    /**
     * @var string[]
    */
    protected $fillable = ['id', 'ids', 'goods_code', 'goods_name', 'unit_price', 'price', 'time', 'release', 'goods_belong', 'max_price', 'is_up', 'productId', 'brand_id', 'order_times', 'order_times_custom', 'timing_is_up', 'goods_type', 'o_type', 'cami_type', 'start_charging','c_id','c_c_id','c_b_id'];

    /**
     * @return HasOne
     */
    public function belong(): HasOne
    {
        return $this->hasOne(InterfaceConfiguration::class, 'id', 'goods_belong');
    }

    /**
     * 循环关联商品
     *
     * @param XnOrder $order
     * @return HasMany
     */
    public function xunhuanRelation(XnOrder $order): HasMany
    {
        $related = $order->order_type == 1 ? XnSupplierGoodsRelation::class : XnSupplierGoodsMerchantRelation::class;
        return $this->hasMany($related, 'g_id', 'id')
            ->when($order->order_type == 2, fn(Builder $builder) => $builder->where('merchant_num', $order->merchant_number));
    }

    /**
     * 组合关联商品
     *
     * @param XnOrder $order
     * @return HasMany
     */
    public function zuheRelation(XnOrder $order): HasMany
    {
        $tick = $order->order_type == 2;
        return $this->hasMany(XnSupplierGoodsFullyChargedRelation::class, 'gid', 'id')
            ->where('type', $tick ? 1 : 2)
            ->when($tick, fn(Builder $builder) => $builder->where('merchantnum', $order->merchant_number));
    }

    /**
     * @param string $order_no
     * @return HasMany
     */
    public function eachOrderChildren(string $order_no): HasMany
    {
        return $this->hasMany(CycleOrder::class, 'pid', 'id')->where('order_no', $order_no);
    }

    /**
     * @param string $merchant_num
     * @return XnSupplierGoods
     * @throws MerchantPriceWithTicketNotSetException
     */
    public function merchantPriceWithTicket(string $merchant_num): XnSupplierGoods
    {
        /** @var XnMerchantGoodsInvoicing|null $invoice * */
        $invoice = XnMerchantGoodsInvoicing::query()->where(['goods_id' => $this->id, 'merchant_num' => $merchant_num])->first();
        if (!$invoice) {
            throw new MerchantPriceWithTicketNotSetException("未配置商户价格");
        }
        return $this->fill(['price' => $invoice->price, 'release' => $invoice->release]);
    }
}
